<?php
declare (strict_types = 1);

namespace app\middleware;

use app\admin\services\AdminService;
use app\Request;
use think\facade\Cache;

class CheckTokenMiddleware implements MiddlewareInterface
{
    /**
     * 处理请求,验证token
     * @param Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle(Request $request, \Closure $next)
    {
        $token = $request->header("token");
        if (!empty($token)) {
            $service = app()->make(AdminService::class);
            $adminUid = $service->parseToken(trim($token));
//            echo $adminUid;die;
            if (!empty($adminUid)) {
                $request->adminUid($adminUid);
                return $next($request);
            }
        }
        api_return(20002);
    }
}
